﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using 纸片人.CalculateControls;
using Windows.UI.Xaml.Navigation;

//https://go.microsoft.com/fwlink/?LinkId=234236 上介绍了“用户控件”项模板

namespace 纸片人.win2dEffectControl.SliderFloder
{
    public sealed partial class Vector2Control : UserControl
    {

        public object Header
        {
            get { return (object)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Header.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(object), typeof(Vector2Control), new PropertyMetadata(null));


        public object OneHeader
        {
            get { return (object)GetValue(OneHeaderProperty); }
            set { SetValue(OneHeaderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for OneHeader.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OneHeaderProperty =
            DependencyProperty.Register("OneHeader", typeof(object), typeof(Vector2Control), new PropertyMetadata(null));



        public object TwoHeader
        {
            get { return (object)GetValue(TwoHeaderProperty); }
            set { SetValue(TwoHeaderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TwoHeader.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TwoHeaderProperty =
            DependencyProperty.Register("TwoHeader", typeof(object), typeof(Vector2Control), new PropertyMetadata(null));


        public double StepFrequency
        {
            get { return (double)GetValue(StepFrequencyProperty); }
            set { SetValue(StepFrequencyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StepFrequency.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StepFrequencyProperty =
            DependencyProperty.Register("StepFrequency", typeof(double), typeof(Vector2Control), new PropertyMetadata(0.1,StepFrequencyChanged));

        private static void StepFrequencyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var vc = d as Vector2Control;
            if(vc != null)
            {
                //vc.oneSlider.StepFrequency = (double)e.NewValue;
                //vc.twoSlider.StepFrequency = (double)e.NewValue;
            }
        }

        public double Maximum
        {
            get { return (double)GetValue(MaximumProperty); }
            set { SetValue(MaximumProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Maximum.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MaximumProperty =
            DependencyProperty.Register("Maximum", typeof(double), typeof(Vector2Control), new PropertyMetadata(100));



        public double Minimum
        {
            get { return (double)GetValue(MinimumProperty); }
            set { SetValue(MinimumProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Minimum.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MinimumProperty =
            DependencyProperty.Register("Minimum", typeof(double), typeof(Vector2Control), new PropertyMetadata(0));




        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Orientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(Vector2Control), new PropertyMetadata(Orientation.Horizontal,OrientationChanged));

        private static void OrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var vc = d as Vector2Control;
            if(vc != null)
            {
                if(e.NewValue!= e.OldValue)
                {
                    if(e.NewValue is Orientation or )
                    {
                        if(or == Orientation.Horizontal)
                        {

                            //Grid.SetRow(vc.twoSlider, 1);
                            //Grid.SetColumn(vc.twoSlider, 1);
                        }
                        else
                        {
                            //Grid.SetRow(vc.twoSlider, 2);
                            //Grid.SetColumn(vc.twoSlider, 0);
                        }

                    }
                }
            }
        }



        public Vector2 Vector2
        {
            get { return (Vector2)GetValue(Vector2Property); }
            set { SetValue(Vector2Property, value); }
        }

        // Using a DependencyProperty as the backing store for Vector2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Vector2Property =
            DependencyProperty.Register("Vector2", typeof(Vector2), typeof(Vector2Control), new PropertyMetadata(new Vector2(),Vector2PropertyChanged));

        private static void Vector2PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as Vector2Control;
            if(control != null)
            {
                //control.oneSlider.Value = control.Vector2.X;
                //control.twoSlider.Value = control.Vector2.Y;
            }
        }

        public delegate void Vector2ChangeHeader(object sender, Vector2 value);
        public event Vector2ChangeHeader Vector2Changing;
        public event Vector2ChangeHeader Vector2Changed;

        public Vector2Control()
        {
            this.InitializeComponent();
        }

        public void SetVector2(Vector2 v)
        {
            //oneSlider.Value = v.X;
            //twoSlider.Value = v.Y;
        }

        public void SetVector2(Point p)
        {
            //oneSlider.Value = p.X;
            //twoSlider.Value = p.Y;
        }

        public void SetVector2(double x,double y)
        {
            //oneSlider.Value = x;
            //twoSlider.Value = y;
        }

        public Vector2 GetVector2()
        {
            //var x =(float) Math.Max(Minimum, Math.Min(oneSlider.Value, Maximum));
            //var y = (float)Math.Max(Minimum, Math.Min(twoSlider.Value, Maximum));

            //return new Vector2(x, y);
            return new Vector2();
        }

        private void Slider_Changing(TextBoxSlider sender, double newValue)
        {
            Vector2 = GetVector2();
            Vector2Changing?.Invoke(this, GetVector2());
        }

        private void Slider_Changed(TextBoxSlider sender, double newValue, double oldValue)
        {
            Vector2 = GetVector2();
            Vector2Changed?.Invoke(this, GetVector2());

        }
    }
}
